/*
student_index.jsp页面
*/
var currentpage=1;
var totalpage=0;
var currentrecord=1;
/**
 * 每页显示条数
 */
var range=5;
var totalcount=0;
/**
 * pp即perpage，正常与range语义一致，
 * 主要为解决数据库记录数小于range而作为range的拷贝
 */
var pp=range;



/**
 * 页面载入自动触发，作获取记录数操作，在异步回调函数中再获取内容
 * 主要是避免range小于数据库记录数而先获取记录数再请求内容
 */ 
$(document).ready(function () {
	$('#spfirst').click(firstfun);
	$('#sppre').click(prefun);
	$('#spnext').click(nextfun);
	$('#splast').click(lastfun);
	$('#queryPlanBtn').click(findPlanNoReportRC);
});

/**
 * 作获取所有预案所有记录数操作
 */
function findPlanNoReportRC(){
	$('#noneTR').hide();
	$('#planTable').show();
	$('#studentTable').children().remove();
	$('#loadingTR').show();
	var url = 'lzm/Plan_findAllPlanRC.action';
	var params = {
			"queryOption['planType']":$('#planType').attr('value'),
			"queryOption['planStatus']":$('#planStatus').attr('value'),
			"queryOption['planScope']":$('#planScope').attr('value'),
			"queryOption['planRegion']":$('#planRegion').attr('value'),
			"queryOption['planGrade']":$('#planGrade').attr('value'),
			planStatus:NOREPORT
	}; 
	jQuery.post(url, params, findPlanNoReportRCRst, 'json');
}

/**
 * 作获取预案所有记录数回调函数
 */
function findPlanNoReportRCRst(data){
	if(data.recordCount==0){
		$('#loadingTR').hide();
		$('#noneTR').show();		
		$('#currpage').text(0);
		$('#countpp').text(0);
		$('#totalpage').text(0);	
		$('#sprecvct').text(0);	
	}else{
		$('#noneTR').hide();
		totalcount=data.recordCount;
		totalpage=Math.ceil(totalcount/range);
		$('#sprecvct').text(totalcount);
		if(totalcount<range){
			pp=totalcount;
		}
		findPartByPage(1,pp);
	}
}


/**
 * 发送异步请求获取分页数据
 */
function findPartByPage(first,max){
	$('#studentTable').children().remove();
	$('#loadingTR').show();
	var url = 'lzm/Plan_findAllPlan.action';
	var params = {
			firstRow : first-1,
			maxRow : max,
			"queryOption['planType']":$('#planType').attr('value'),
			"queryOption['planStatus']":$('#planStatus').attr('value'),
			"queryOption['planScope']":$('#planScope').attr('value'),
			"queryOption['planRegion']":$('#planRegion').attr('value'),
			"queryOption['planGrade']":$('#planGrade').attr('value'),
			planStatus:NOREPORT
	}; 
	jQuery.post(url, params, findPartByPageRst, 'json');
}


function delPlanById(myself){	
	var timestamp = Date.parse(new Date());
	$.get("lzm/Plan_delPlanById.action?ts="+timestamp+"&PId="+myself.next().text(),null,delPlanByIdRst);
}

function delPlanByIdRst(data){
	if(data.ajaxRst=SUCCESS){
		alert("删除数据成功");
		findPlanNoReportRC();
	}else{
		alert("删除数据失败");
	}
}


/**
 * 发送异步请求获取分页数据回调函数
 */
function findPartByPageRst(data){
	$.each(data.planList,function(i,n){
		fillDatatoHTML(n.PId,n.PName,n.tbPlanGrade.pgName,n.tbPlanStatus.pstName,n.tbPlanType.ptName);
	});
	$('.planDetail').click(function () {
		setCookie(PId,$(this).next().text());
		window.location.href="/ag/lzm/query_plan_no_report.jsp";
	});
	$('.editPlan').click(function () {
		setCookie(PId,$(this).next().text());
		window.location.href="/ag/lzm/edit_plan_no_report.jsp";
	});
	$('.delPlan').click(function () {
		delPlanById($(this));
	});
	calculatepage();
}

/**
 * DOM操作，动态生成HTML来显示内容
 */
function fillDatatoHTML(tbPlanPId,ptName,pgName,pstName,prName){	
	var strhtml;	
	strhtml="<tr><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE1'>";
	strhtml+="<input name='checkbox' type='button' class='STYLE2 planDetail myBtn' value='查看' /><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span>";
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF' class='STYLE2'>";
	strhtml+="<div align='center' class='STYLE2 STYLE1'>";
	strhtml+=tbPlanPId;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=ptName;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=pgName;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=pstName;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center' class='STYLE2 STYLE1'>";
	strhtml+=prName;
	strhtml+="</div></td><td height='18' bgcolor='#FFFFFF'><div align='center'>";
	strhtml+="<img src='images/037.gif' width='9' height='9' /><span class='STYLE1'>";
	strhtml+=" [</span><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><a href='javascript:;' class='editPlan'>编辑</a><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><span class='STYLE1'>]</span></div></td>";
	strhtml+="<td height='18' bgcolor='#FFFFFF'><div align='center'><span class='STYLE2'>";
	strhtml+="<img src='images/010.gif' width='9' height='9' /> </span><span class='STYLE1'>";
	strhtml+="[</span><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><a href='javascript:;' class='delPlan'>删除</a><span style='display:none'>";
	strhtml+=tbPlanPId;
	strhtml+="</span><span class='STYLE1'>]</span></div></td></tr>";	
	$('#loadingTR').hide();
	$('#studentTable').append(strhtml);			
}

/**
 * 计算页数，实际上是显示页数的变化
 * 当前是第1页   共1页   首页 上一页 下一页 末页   本页显示23条记录 
 */
function calculatepage(){
	$('#currpage').text(currentpage);
	$('#countpp').text(pp);
	$('#totalpage').text(totalpage);	
}

/**
 * 首页处理函数
 */
function firstfun(){
	if(currentpage!=1){
		findPartByPage(1,range);
		currentpage=1;
		currentrecord=1;
		pp=range;
	}
}

/**
 * 上一页处理函数
 */
function prefun(){
	if(currentpage!=1){
		currentrecord-=range;		
		findPartByPage(currentrecord,range);
		currentpage--;
		pp=range;
	}
}

/**
 * 下一页处理函数
 */
function nextfun(){
	if(currentpage!=totalpage){	
		var t=Math.ceil(totalcount%range);
		currentrecord+=range;	
		if(currentpage!=totalpage-1){			
			findPartByPage(currentrecord,range);
			pp=range;
		}
		else{
			if(t==0) t=range;
			findPartByPage(currentrecord,t);
			pp=t;
		}		
		currentpage++;		
	}
}

/**
 * 末页处理函数
 */
function lastfun(){
	if(currentpage!=totalpage){
		var t=Math.ceil(totalcount%range);
		if(t==0) t=range;
		currentrecord=totalcount-t+1;
		findPartByPage(currentrecord,t);
		currentpage=totalpage;		
		pp=t;
	}
}
function displayPlanTree(){
	window.location.href="find_plan_container.jsp";
}
/*
function delStudentById(myself){	
	var timestamp = Date.parse(new Date());
	$.get("../teacher/teacher_delStudentById.action?ts="+timestamp+"&SNo="+myself.prev().text(),null,delStudentByIdRst);
}

function delStudentByIdRst(data){
	if(data.ajaxRst==SUCCESS){
		operateSucc("teacher/student_index.jsp");
	}else{
		alert("删除学生失败");
	}
}*/